﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKFloor.Forms;

namespace YJKFloor.FloorEditor
{
	public class FloorThicknessEditer
	{
		public FloorThicknessEditer(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.m_Doc = this.m_Revit.Application.ActiveUIDocument.Document;
		}

		public bool NewFloorType(FloorType refFloorType, ref FloorType newFloorType, ref string message)
		{
			List<FloorThicknessInfos> floorThicknessInfos = this.GetFloorThicknessInfos(refFloorType);
			List<string> allFloorTypeNames = this.GetAllFloorTypeNames();
			EditFloorThicknessForm editFloorThicknessForm = new EditFloorThicknessForm(refFloorType.Name, floorThicknessInfos, allFloorTypeNames);
			if (editFloorThicknessForm.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			bool result;
			try
			{
				newFloorType = this.GetFloorType(refFloorType, editFloorThicknessForm.NewFloorTypeName, floorThicknessInfos, ref message);
				if (newFloorType == null)
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch (Exception)
			{
				message = "新类型命名有误";
				result = false;
			}
			return result;
		}

		private FloorType GetFloorType(FloorType currentFloorType, string newFloorTypeName, List<FloorThicknessInfos> floorThicknessInfos, ref string message)
		{
			FloorType floorType = currentFloorType.Duplicate(newFloorTypeName) as FloorType;
			CompoundStructure compoundStructure = floorType.GetCompoundStructure();
			if (compoundStructure != null)
			{
				IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
				for (int i = 0; i < layers.Count; i++)
				{
					layers[i].Width = AssistFunc.mmToFeet(floorThicknessInfos[i].m_Width);
				}
				compoundStructure.SetLayers(layers);
				floorType.SetCompoundStructure(compoundStructure);
			}
			return floorType;
		}

		private List<string> GetAllFloorTypeNames()
		{
			List<string> list = new List<string>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			ICollection<Element> collection = filteredElementCollector.OfClass(typeof(FloorType)).ToElements();
			foreach (Element element in collection)
			{
				FloorType floorType = element as FloorType;
				if (floorType.Category.Id.IntegerValue == -2000032)
				{
					list.Add(floorType.Name);
				}
			}
			return list;
		}

		private double GetThicknessWithFloorType(FloorType floorType)
		{
			double result = 0.0;
			Parameter parameter = floorType.get_Parameter(BuiltInParameter.FLOOR_ATTR_THICKNESS_PARAM);
			if (parameter != null)
			{
				return parameter.AsDouble();
			}
			Parameter parameter2 = floorType.get_Parameter(BuiltInParameter.FLOOR_ATTR_DEFAULT_THICKNESS_PARAM);
			if (parameter2 != null)
			{
				return parameter2.AsDouble();
			}
			return result;
		}

		private List<FloorThicknessInfos> GetFloorThicknessInfos(FloorType floorType)
		{
			List<FloorThicknessInfos> list = new List<FloorThicknessInfos>();
			CompoundStructure compoundStructure = floorType.GetCompoundStructure();
			if (compoundStructure == null)
			{
				double num = this.GetThicknessWithFloorType(floorType);
				num = AssistFunc.feetToMM(num);
                FloorThicknessInfos item = new FloorThicknessInfos((MaterialFunctionAssignment)1, "结构", num);
				list.Add(item);
				return list;
			}
			IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
			foreach (CompoundStructureLayer compoundStructureLayer in layers)
			{
				MaterialFunctionAssignment function = compoundStructureLayer.Function;
				string name = "";
				MaterialFunctionAssignment materialFunctionAssignment = function;
                switch ((int)materialFunctionAssignment)
				{
				case 1:
					name = "结构";
					break;
				case 2:
					name = "衬底";
					break;
				case 3:
					name = "保温层/空气层";
					break;
				case 4:
					name = "面层1";
					break;
				case 5:
					name = "面层2";
					break;
				default:
                    if ((int)materialFunctionAssignment != 100)
					{
                        if ((int)materialFunctionAssignment == 200)
						{
							name = "结构装饰";
						}
					}
					else
					{
						name = "涂膜层";
					}
					break;
				}
				double width = AssistFunc.feetToMM(compoundStructureLayer.Width);
				FloorThicknessInfos item2 = new FloorThicknessInfos(function, name, width);
				list.Add(item2);
			}
			return list;
		}

		private ExternalCommandData m_Revit;

		private Document m_Doc;
	}
}
